package smlab.view.ui
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	import smlab.view.ui.UIEvent.UIEvent;

	public class UIComponent_SM extends Sprite
	{
		protected var _skin:DisplayObject;
		protected var _isShow:Boolean=true;
		protected var _hasScreenBg:Boolean=false;
		private var _screenBg:Sprite;
		private var _screenBgColor:uint=0;
		private var _screenBgAlphaN:Number=0.3;
		
		public function UIComponent_SM()
		{
			super();
		}
		
		/**
		 * 设置面板皮肤
		 * @param d
		 * 
		 */
		public function set skin(d:DisplayObject):void{
			_skin=d;
			if(_skin !=null && _skin !=this){
				this.addChild(skin);
			}
		}
		
		public function get skin():DisplayObject{
			return _skin;
		}
		
		public function set isShow(b:Boolean):void{
			_isShow=b;
		}
		
		public function get isShow():Boolean{
			return _isShow;
		}
		
		public function show():void
		{
			this.visible=true;
			this.isShow=true;
			
			drawMaskBg();
		}
		
		public function hide():void
		{
			this.isShow=false;
			this.visible=false;
			removeScreenBG();
		}
		
		/**
		 * 设置背景遮挡
		 * @param hasScreenBg		是否有背景遮挡层（用于阻止用户点击此面板以下的任何物品）
		 * @param color				背景遮挡层的 uint 值
		 * @param alphaN			背景遮挡层的 alpha 值（0-1）
		 * @drawImmediately			是否立刻绘制（如果false则下次show的时候绘制）	
		 * 
		 */
		public function setScreenBg(hasScreenBg_:Boolean=false,color:uint=0xffffff,alphaN:Number=0.3,drawImmediately:Boolean=false):void
		{
			_screenBgColor=color;
			_screenBgAlphaN=alphaN;
			_hasScreenBg=hasScreenBg_;
			
			if(drawImmediately){
				drawMaskBg();
			}
		}
		
		public function set hasScreenBg(b:Boolean):void
		{
			_hasScreenBg=b;
			drawMaskBg();
		}
		
		public function get hasScreenBg():Boolean
		{
			return _hasScreenBg;
		}
		
		private function drawMaskBg():void
		{
			if(hasScreenBg==false){
				removeScreenBG();
				return;
			}
			
			if(!_screenBg){
				_screenBg=new Sprite;
			}else{
				_screenBg.graphics.clear();
			}
			
			_screenBg.graphics.beginFill(_screenBgColor,_screenBgAlphaN);
			_screenBg.graphics.drawRect(0,0,10,10);
			_screenBg.graphics.endFill();
			
			if(this.parent){
				if(this.parent.stage != null){
					var p:Point=globalToLocal(new Point(0,0));
					_screenBg.x=p.x;
					_screenBg.y=p.y;
					_screenBg.width=this.parent.stage.stageWidth;
					_screenBg.height=this.parent.stage.stageHeight;
				}
			}
			
			addChildAt(_screenBg,0);
			_screenBg.visible=true;
		}
		
		private function removeScreenBG():void
		{
			if(_screenBg){
				if(_screenBg.parent){
					_screenBg.parent.removeChild(_screenBg);
				}
				_screenBg=null;
			}
		}
	}
}